
#include "../stdafx.h"
#include "Language.h"
#include "../Util/Util.h"
#include "Config.h"
#include "../debug_new.h"

CLanguage::CLanguage(void)
{
}

CLanguage::~CLanguage(void)
{
	m_mapItems.clear();
}

CLanguage* CLanguage::g_pInstance = NULL;
wstring CLanguage::g_strLangFile = L"English.xml";

CLanguage* CLanguage::GetInstance()
{
	if(NULL == g_pInstance)
	{
		g_pInstance = new CLanguage();
		
		if(!g_pInstance->InitLanMap())
		{
			delete g_pInstance;
			g_pInstance = NULL;
		}
	}

	return g_pInstance;
}

BOOL CLanguage::InitLanMap()
{
	CXMLParser xml;

#ifdef XML_FROM_FILE

	g_strLangFile = CConfig::GetInstance()->GetLanguage();

	wstring strPath = CUtil::GetCurrentDirectory();
	strPath += L"\\Config\\Language\\";
	strPath += g_strLangFile;

	if(!xml.Load(strPath))
	{
		return FALSE;
	}

#else

	BOOL bRet = FALSE;

	g_strLangFile = CConfig::GetInstance()->GetLanguage();

	bRet = CUtil::InitResXML(xml,g_strLangFile,L"LAN");

	if(!bRet)
	{
		return FALSE;
	}

#endif

	m_mapItems.clear();
	
	while(xml.FindChildElem())
	{
		m_mapItems.insert(map<wstring,wstring>::value_type(xml.GetChildTagName(), xml.GetChildAttrib(L"TEXT")));
	}

	return TRUE;
}

wstring CLanguage::GetText(wstring strKey)
{
	if(NULL == g_pInstance)
	{
		return L"";
	}
	return m_mapItems[strKey];
}

BOOL CLanguage::SetLanguageFile(wstring strFileName)
{
	//CXMLParser xml;
	//wstring strPath = CUtil::GetCurrentDirectory();
	//strPath += L"\\Config\\Language\\";
	//strPath += g_strLangFile;

	//if(!xml.Load(strPath))
	//{
	//	return FALSE;
	//}

	g_strLangFile = strFileName;

	return InitLanMap();
}

void CLanguage::Free()
{
	if(g_pInstance)
	{
		delete g_pInstance;
		g_pInstance = NULL;
	}

	GetInstance();
}